﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using 约苗秒杀.Models;
using 约苗秒杀.Service;

namespace 约苗秒杀
{
    public partial class Form1 : Form
    {
        HttpService httpservice;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text)) SendMeg("数据未填写");
            Config.cookies = textBox2.Text;
            Config.tk = textBox1.Text;
            button2.Enabled = true;
            if (!File.Exists("config-tk.txt"))
            {
                File.Create("config-tk.txt").Close();
            }
            using (StreamWriter sw = new StreamWriter("config-tk.txt", false))
            {
                sw.WriteLine(JsonConvert.SerializeObject(new ConfigTkAndCookie{ Tk= Config.tk,Cookie=Config.cookies })); 
            }
        }
        SecKillService sevice;
        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("config-tk.txt"))
            {
                using (StreamReader sr = new StreamReader("config-tk.txt"))
                {
                    var tData = JsonConvert.DeserializeObject<ConfigTkAndCookie>(sr.ReadToEnd());
                    Config.cookies = tData.Cookie;
                    Config.tk = tData.Tk;
                    textBox2.Text = Config.cookies;
                    textBox1.Text = Config.tk;
                }
            }
            
            sevice = new SecKillService(this);
            CheckForIllegalCrossThreadCalls = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            httpservice = new HttpService(this);
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
            {
                Config.cookies = textBox2.Text;
                Config.tk = textBox1.Text;
                button2.Enabled = true;
                SendMeg("检测到tk和cookies存在默认配置 以默认设置配置数据");
            } 
            comboBox1.DataSource = ParseUtil.getChildren("").Select(i => new BindInfo { Key = i.getName(), Value = i.getValue() }).ToList();
            comboBox1.ValueMember = "Value";
            comboBox1.DisplayMember = "Key";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            comboBox2.DataSource = ParseUtil.getChildren(((BindInfo)((ComboBox)sender).SelectedItem).Key).Select(i => new BindInfo { Key = i.getName(), Value = i.getValue() }).ToList();
            comboBox2.ValueMember = "Value";
            comboBox2.DisplayMember = "Key";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Config.regionCode = ((BindInfo)comboBox2.SelectedItem).Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var chegnyuan = new 成员(this);
            chegnyuan.Show();
            button3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            var datas = httpservice.getVaccineList();
            if (datas == null)
            {
                SendMeg($"刷新医院列表失败");
                return;
            }
            SendMeg($"刷新医院列表成功 {((BindInfo)comboBox2.SelectedItem).Key}当前有{datas.Count}个医院有秒杀活动");
            foreach (var item in datas)
            {
                int index = this.dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = item.Id;
                dataGridView1.Rows[index].Cells[1].Value = item.Name;
                dataGridView1.Rows[index].Cells[2].Value = item.VaccineName;
                dataGridView1.Rows[index].Cells[3].Value = item.StartTime;
            }
            button4.Enabled = true;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择医院");
                return;
            }
            var index = dataGridView1.CurrentRow.Index;
            if (index < 0 || dataGridView1.Rows[index].Cells[0].Value==null)
            {
                MessageBox.Show("请选择医院");
                return;
            }
            sevice.startSecKill(int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString()), dataGridView1.Rows[index].Cells[3].Value.ToString());
        }

        public void SendMeg(string msg)
        {
            textBox3.Text += msg + "\r\n\r\n";
            this.textBox3.Focus();//获取焦点
            this.textBox3.Select(this.textBox3.TextLength, 0);//光标定位到文本最后
            this.textBox3.ScrollToCaret();//滚动到光标处
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var t = button6.Text;
            button6.Enabled = false;
            button6.Text = "正在遍历中";
            Task.Run(() =>
            {
                var count = 0;
                foreach (BindInfo item in comboBox1.Items)
                {
                    var subdata = ParseUtil.getChildren(item.Key).Select(i => new BindInfo { Key = i.getName(), Value = i.getValue() }).ToList();
                    foreach (var item1 in subdata)
                    {
                        var datas = httpservice.getVaccineList(item1.Value);
                        if (datas.Count > 0)
                        {
                            foreach (var item2 in datas)
                            {
                                SendMeg($"{item1.Key}--{item2.Name}--{item2.StartTime}");
                                count++;
                            }
                        }
                        Thread.Sleep(2000);
                    }
                }
                SendMeg($"遍历完成 查询到{count}个医院");
                button6.Text = t;
                button6.Enabled = true;
            });

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        { 
            Config.regionCode = ((BindInfo)comboBox2.SelectedItem).Value;

            dataGridView1.Rows.Clear();
            var datas = httpservice.getVaccineList();
            if (datas == null)
            {
                SendMeg($"刷新医院列表失败");
                return;
            }
            SendMeg($"刷新医院列表成功 {((BindInfo)comboBox2.SelectedItem).Key}当前有{datas.Count}个医院有秒杀活动");
            foreach (var item in datas)
            {
                int index = this.dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = item.Id;
                dataGridView1.Rows[index].Cells[1].Value = item.Name;
                dataGridView1.Rows[index].Cells[2].Value = item.VaccineName;
                dataGridView1.Rows[index].Cells[3].Value = item.StartTime;
            }
            button4.Enabled = true;
        }
    }

    public class BindInfo
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}
